package com.pipodi.itasamobile.connections;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.pipodi.itasamobile.utils.LoginVariables;

import android.os.AsyncTask;
import android.util.Log;

public class ItaSALogin extends AsyncTask<String, String, String> {

	private String username;
	private String password;
	private DefaultHttpClient httpClient = new DefaultHttpClient();
	private HttpPost httpPost = null;
	private String cookies;

	public ItaSALogin(String username, String password) {
		this.username = username;
		this.password = password;
	}

	@Override
	protected String doInBackground(String... params) {
		httpPost = new HttpPost("http://www.italiansubs.net");
		httpPost.setHeader("User-Agent",
				"ItaSA Mobile Application test by Pipodi");
		httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		Document doc = null;
		try {
			doc = Jsoup.connect("http://www.italiansubs.net").get();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Element loginform = doc.getElementById("form-login");
		Elements inputElements = loginform.getElementsByTag("input");
		for (Element inputElement : inputElements) {
			String key = inputElement.attr("name");
			String value = inputElement.attr("value");
			if (key.equals("username"))
				value = username;
			else if (key.equals("passwd"))
				value = password;
			pairs.add(new BasicNameValuePair(key, value));
			Log.i("HTMLParsing", "Name: " + key + "|| Value: " + value);
		}
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairs));
			httpClient.execute(httpPost);
			Header[] cookies = httpPost.getHeaders("Set-Cookie");
			for (Header head : cookies){
				Log.i("HEADER", head.toString());
			}
			LoginVariables.loginCookies = httpClient.getCookieStore()
					.getCookies();
			for (Cookie cookie : LoginVariables.loginCookies) {
				Log.i("COOKIE", cookie.toString());
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		LoginVariables.logged = true;
		Log.i("LOGINPOST", "Fine postExecute login");
	}
}
